import axios from 'axios'

let store = {
    state: {
        user: {}
    }
}

class HttpRequest {
    constructor(baseUrl) {
        this.baseUrl = baseUrl
        this.queue = {}
    }

    getInsideConfig() {
        const config = {
            baseURL: this.baseUrl,
            headers: {
            }
        }
        if (store.state.user.token) {
            config.headers['Zciss-Token'] = store.state.user.token
        }
        return config
    }

    destroy(url) {
        delete this.queue[url]
        if (!Object.keys(this.queue).length) {
        //
        }
    }

    interceptors(instance, url) {
// 请求拦截
        instance.interceptors.request.use(
            config => {
                if (!Object.keys(this.queue).length) {
                //
                }
                this.queue[url] = true
                return config
            },
            error => {
                return Promise.reject(error)
            }
        )
// 响应拦截
        instance.interceptors.response.use(
            res => {
                this.destroy(url)
                const {data, status} = res
                return {data, status}
            },
            error => {
                this.destroy(url)
                return Promise.reject(error)
            }
        )
    }

    request(options) {
        const instance = axios.create()
        options = Object.assign(this.getInsideConfig(), options)
        this.interceptors(instance, options.url)
        return instance(options)
    }
}

export default HttpRequest
